Skip to content

feat: protect and unprotect a copy from the copy page - #175

Merged
Reilley64 merged 2 commits into
mainfrom
feat/147-protect-copy
Oct 14, 2026
Merged

Reilley64 merged 2 commits into
mainfrom
feat/147-protect-copy

Conversation

@Reilley64

Copy link
Copy Markdown
Owner

Closes #147

Summary

The copy page's actions menu now has Protect for a free copy and Unprotect for a protected copy, before Delete copy. The Protected chip changes at once. A protected copy's binder tile shows a warning shield before the set icon.

/copy (app/copy.tsx)
  <Stack.Toolbar placement="right">
    copyActionsMenu({ copy, onSetProtected, onDelete })   ••• "More"
      Unprotect                 when copy.protected
      Protect                   when not protected and copy.free
      Delete copy               destructive (#174)
  changeProtected(isProtected)
    setCopyProtected(copyId, isProtected)  -> rpc set_copy_protected({ request })
      failed  -> alert "Could not protect this card" / "Could not unprotect this card", "Check your connection."
      done or missing -> reload()  (missing shows "This card is no longer in your collection.")
set_copy_protected(request)        security definer, authenticated only
  caller := private.caller_user_id()
  copy_id := private.checked_copy_id(request, ['protected'])   shared with delete_copy
  protected: required boolean
  lock the caller's copy, else not_found on copy_id
  protecting a copy that is not free -> copy_not_free on copy_id
  update that copy only -> { copy_id, protected }
 <BinderTile>
   accessibilityLabel: name, set and number, finish, condition,
+    protected,
     price
   <CopyMeta
+    isProtected   11 px shield.fill in warning, before the set icon
   />

Other changes:

  • Migration 20261031120000_protect_and_unprotect_a_copy.sql. It sorts after 20261031100000_delete_a_copy.sql and reuses its private.checked_copy_id and private.raise_copy_not_found.
  • copy-writes.ts maps both writes to one CopyWriteResult through resultOf.
  • The test user's sample collection protects one copy: Protected 1 sample copy.
  • theme.ts gains sizes.protectedSymbol (11). The primitive catalog has a protected binder tile.
  • README.md gains Protect a copy. DESIGN.md (Collection, Copy page, Binder tile) gains the menu items, the tile shield, and the VoiceOver word. docs/agents/local-development.md says the sample has one protected copy.
  • The fake Supabase client shares one copyNotFound answer for the read and both writes.

Evidence

Tests:

  • Database (seam a), supabase/tests/copy-protection.test.ts, 14 tests. Protect and unprotect change only one copy, among two copies of one collection entry and a copy of another card. Doing it twice changes nothing. Refused requests name their field. An unknown copy and another user's copy are not_found, and the other user's copy stays unprotected. An anonymous caller and the secret key are refused. A direct update of copies is refused. test-user.test.ts checks that the sample collection has one protected copy and still 30 collection entries. The full database suite passed before the rebase onto feat: delete a copy from the copy page #174 (733 tests). After it, copy-protection, copy-deletes, and test-user pass (36 tests), and CI runs the full suite.
  • App (seam c), apps/mobile/test/copy-protection.test.tsx, 10 tests: the menu of a free copy, a protected copy, and a copy that is not free. Protect protects only the opened copy and shows the chip. Unprotect removes it. After Protect, the binder tile shows the shield and the other tile does not. A copy that is gone shows the missing copy alert and goes back. A failed write says so and keeps the copy. A protected tile has the 11 px warning shield before the set icon, and VoiceOver hears "protected" before the price. molecules.test.tsx covers the CopyMeta shield. Full Jest run: 514 tests pass. Lint and type checks pass.

Mutation checks:

  • set_copy_protected updates where copy.user_id = caller without the copy ID -> protecting a copy protects only that copy, unprotecting a copy unprotects only that copy, and protecting a protected copy again changes nothing fail. Restored, they pass.
  • Unprotect calls the write with protected: true -> Unprotect unprotects this copy and removes the Protected chip fails. Restored, it passes.
  • The binder tile drops isProtected -> the two tile shield tests fail. Restored, they pass.

copy_not_free has no test. Every copy is free until decks exist (spec 6), so no copy can reach it yet.

Simulator (shared iPhone 16e, test sign-in, sample collection with one protected copy):

  1. is:protected finds the one protected copy, Smothering Tithe CMM 473 etched. Its tile shows the warning shield before the set icon. VoiceOver reads "Smothering Tithe, CMM 473, etched, protected, $72.72".
  2. Its copy page shows the Protected chip. The ••• menu shows Unprotect and Delete copy in red.
  3. Unprotect removes the chip. The menu then shows Protect and Delete copy.
  4. Protect brings the chip back. An edge swipe goes back to the binder, and the tile shows the shield.

Merge Danger

Door: two-way

The migration adds set_copy_protected. It replaces no function.

Blast Radius: small

The binder tile's VoiceOver label gains "protected" for protected copies.

Decisions to check:

  • A failed write shows a native alert "Could not protect this card" or "Could not unprotect this card" with "Check your connection.", like the failed delete. The spec has no copy for it, so DESIGN.md now holds it.
  • copy_not_free is on the copy_id field, because the copy is what is not free.
  • On the Simulator, the keyboard of the Collection search bar came back over the copy page when the ••• menu opened after a search. This PR does not change it, and it needs its own check.

@Reilley64
Reilley64 merged commit 303742d into main Oct 14, 2026
3 checks passed
@Reilley64
Reilley64 deleted the feat/147-protect-copy branch October 14, 2026 12:54
Reilley64 added a commit that referenced this pull request Oct 14, 2026
Closes #149

## Summary

```diff
 Copy page (apps/mobile/src/app/copy.tsx)
   chips
+  User tags: one disclosure row, chips or "Add user tags", chevron
+    -> pushes the user tag picker (src/app/user-tag-picker.tsx)
   Price
   Rules

+User tag picker
+  "User tags" title, copy line, "Find or create a user tag" field (64 characters)
+  Create "<name>" row  -> create_user_tag, then set_copy_user_tag
+  check rows           -> set_copy_user_tag({ copy_id, user_tag_id, tagged })
+  "No user tags yet. Type a name to create one."
```

```diff
 supabase/migrations/20261031130000_put_user_tags_on_copies.sql
+user_tags (id, user_id, name trimmed 1-64, created_at), unique (user_id, lower(name))
+copy_user_tags (user_id, copy_id, user_tag_id), cascades from copies and user_tags
+create_user_tag(request)       security definer, user_tag_exists on name
+set_copy_user_tag(request)     security definer, idempotent, returns nothing, not_found on copy_id or user_tag_id
+list_user_tags()               security invoker, copy_count, sorted by name ignoring case
 get_copy(query)
+  user_tags: [{ user_tag_id, name }] sorted by name ignoring case
```

- Write functions take one jsonb argument named `request`, like
`set_copy_protected` (#175) and `delete_copy` (#174).
`set_copy_user_tag` reuses `private.checked_copy_id` and
`private.raise_copy_not_found`, and the app write goes through
`collection/copy-writes.ts`.
- The migration sorts after protect (20261031120000).
- `PushedScreen` keeps the keyboard up when a tap lands on a control, so
the first tap on a picker row acts. The Simulator showed that the first
tap only closed the keyboard before this change.
- New primitives: `ListRow` with `checked` (a check row, read as a
checkbox) and `DisclosureRow`. Both are in the primitive catalog and
`molecules.test.tsx`.
- `useFocusedRead` now holds the focus reload that `useCopy` and
`useUserTags` share. `copyChipLabels` gives the copy page chips and the
picker's copy line.
- The test user's sample collection adds "Signed" on one copy and "Trade
binder" on three copies.
- README lists `user_tags` and `copy_user_tags` as per-user data and
describes the functions. DESIGN.md describes the User tags row, the user
tag picker, check rows, and disclosure rows.

## Evidence

Database tests at seam (a), `supabase/tests/user-tags.test.ts` (32
tests), plus `copy-reads`, `per-user-reads`, and `test-user`:

```text
creating a user tag
  creates a user tag with the trimmed name as typed
  a name is trimmed of tabs and line breaks too
  a name that matches a user tag ignoring case is refused with user_tag_exists on name
  empty, only spaces, 65 characters, not a string, unknown field -> invalid_argument on the field
putting a user tag on a copy
  puts a user tag on one copy and takes it off, and doing either twice changes nothing
  putting a user tag on a copy and taking it off change no collection entry
  not_found on copy_id or user_tag_id
the user tag list
  sorted by name ignoring case, with how many copies have each
  deleting a copy (delete_copy) removes its user tag links, and the count drops
access
  another user's user tags and links are not visible and cannot be used
  an anonymous caller and the secret key are refused; direct writes are refused
```

Component tests at seam (c), `apps/mobile/test/copy-page.test.tsx`: the
User tags row and its VoiceOver label, the picker title, copy line, 64
character field, edge swipe back, matching rows with checks and counts,
toggling, Create, the empty text, a failed change, a failed read, and a
copy that no longer exists. Binder tiles still show no user tags.

Mutation checks (each test went red, then green after restoring):

| Change | Test that went red |
| --- | --- |
| drop the unique index on `(user_id, lower(name))` | a name that
matches a user tag ignoring case is refused |
| `user_tags` read policy `using (true)` | a user cannot see another
user's user tags or links |
| `copy_user_tags` copy foreign key without cascade | deleting a copy
removes its user tag links |
| app match without `toLowerCase()` | a name that matches no user tag
ignoring case offers to create it |
| `ListRow` without `aria-checked` | tapping a row adds the user tag to
this copy |

Local runs after the rebase on #174 and #175: `bun test` in `supabase`
783 pass; Jest 530 pass; lint and typecheck clean.

**Simulator** (shared iPhone 16e, development build, test sign-in, local
Supabase, sample collection). The screenshots are on the owner's Mac in
`/tmp/mtgscan-149/`:
- `copy-page-user-tags.png`: the protected sample copy with the User
tags heading and the row with "Signed" and "Trade binder" chips and the
chevron.
- `user-tag-picker.png`: "User tags", the line "Smothering Tithe · CMM ·
473 · Etched · NM · English", the field, and lime checks on "Signed, 1
copy" and "Trade binder, 3 copies".
- `user-tag-picker-created.png`: after "Mint" was typed and **Create
"Mint"** tapped with the keyboard up: "Mint, 1 copy" checked, the field
cleared, "Signed" unchecked with an empty circle.
- `copy-page-after.png`: back with the edge swipe, the row shows "Foil
binder", "Mint", and "Trade binder".
- `copy-page-add-user-tags.png`: a copy with no user tags shows "Add
user tags" with the chevron.

Owner check on the phone: the edge swipe back from the picker and the
keyboard behavior.

## Merge Danger

**Door:** two-way

New tables and functions only. `get_copy` gains one field.

**Blast Radius:** small

The copy page gains a section. Rolling back drops the two new tables and
their data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant